package edu.pku.sei.transformation.qvt.adapter.standardqvt;

import java.util.ArrayList;
import java.util.List;

import edu.pku.sei.transformation.editor.model.variable.ObjectVariable;
import edu.pku.sei.transformation.qvt.rule.IConnectionTemplate;
import edu.pku.sei.transformation.qvt.rule.IObjectTemplate;

public class ObjectTemplateAdapter implements IObjectTemplate {
	public ObjectVariable element;
	public ArrayList<IConnectionTemplate> outgoings = new ArrayList<IConnectionTemplate>();
	private boolean simpleStyle = false;

	public ObjectTemplateAdapter(ObjectVariable v) {
		element = v;
	}

	@Override
	public List<String> getExpressions() {
		List<String> list = new ArrayList<String>();
		for (Object exp : element.getExpressions()) {
			list.add(exp.toString());
		}
		return list;
	}

	@Override
	public String getName() {
		return element.getName();
	}

	@Override
	public List<IConnectionTemplate> getOutGoings() {
		return outgoings; 
	}

	@Override
	public String getType() {
		return element.getType().toString();
	}

	@Override
	public String getContent() {
		if (simpleStyle)
			return getSimpleContent();
		else {
			simpleStyle = true;
			return getComplexContent();
		}
	}

	private String getSimpleContent() {
		return getName();
	}

	private String getComplexContent() {
		String s = "";
		s += getName() + ":" + getType();
		s += "{";
		boolean haveExp = false;
		for (String exp : getExpressions()) {
			if (haveExp) {
				s += ", ";
			} else {
				haveExp = true;
			}

			if (exp.endsWith(";")) {
				s += exp.substring(0, exp.length() - 1);
			} else {
				s += exp;
			}
		}
		for (IConnectionTemplate con : getOutGoings()) {
			if (haveExp) {
				s += ", ";
			} else {
				haveExp = true;
			}

			s += con.getContent();
		}
		s += "}";
		return s;
	}

}
